﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ciphersys
{
    class Program
    {
        public static string message = null;
        public static string key = null;
        public static string mode = null;
        public static string ciphertext = null;
        enum convertable
        {
            a = 01,
            b = 02,
            c = 03,
            d = 04,
            e = 05,
            f = 06,
            g = 07,
            h = 08,
            i = 09,
            j = 10,
            k = 11,
            l = 12,
            m = 13,
            n = 14,
            o = 15,
            p = 16,
            q = 17,
            r = 18,
            s = 19,
            t = 20,
            u = 21,
            v = 22,
            w = 23,
            x = 24,
            y = 25,
            z = 26,
            A = 00
        }  //枚举对应映射
        public static string cipher(string key, string group,int number)
        {
            string[] k = new string[number];
            string[] g = new string[number];
            string[] c = new string[number];
            string cipher = null;
            for (int i = 0; i < number; i++)
            {
                k[i] = key.Substring(2 * i, 2);
                g[i] = group.Substring(2 * i, 2);
                c[i] = (int.Parse(k[i]) * int.Parse(g[i])).ToString();
                    cipher = cipher + " " + c[i];
            }
            return cipher;
        }  //加密算法
        public static string decrypt(string key, string cipher)
        {
            if (int.Parse(cipher) != 0)
            {
                string message = null;
                message = string.Format("{0:D2}", (int.Parse(cipher) / int.Parse(key)));
                return message;
            }
            else
            {
                string message = "00";
                return message;
            }
        }   //解密算法
        public static string convert(char input)
        {
            string output = null;
            int outputs = (int)Enum.Parse(typeof(convertable), input.ToString());
            output = string.Format("{0:D2}", outputs);
            return output; 
        }  //字母转数字
        public static int Substringcount(string str, string substring)
        {
            int count = 0;
            if (str.Contains(substring))
            {
                string strReplaced = str.Replace(substring, "");
                count = (str.Length - strReplaced.Length) / substring.Length;
            }
            return count;
        }
        

    
        static void Main(string[] args)
        {
            Console.WriteLine("Choose the mode you want to use,1 as decrypt and 2 as encrypt");
            mode = Console.ReadLine();
            if (mode == "1")
            {
                Console.WriteLine("Enter Cipher Message you want to decrypt");
                ciphertext = Console.ReadLine();
                Console.WriteLine("Enter Encryption Key");
                key = Console.ReadLine();
                char[] dkeys = key.ToCharArray();
                int cipgroupsnumber = Substringcount(ciphertext, " ");
                int keyused = cipgroupsnumber / key.Length;
                string[] ciphertextsplited = ciphertext.Split(' ');
                string parseddkey = null;
                string parseddmessage = null;
                for (int i = 0; i < key.Length; i++)
                {
                    parseddkey = parseddkey + convert(dkeys[i]);
                }
                for (int j = 0; j < keyused; j++)
                {
                    for (int k = 0; k < key.Length; k++)
                    {
                        int num = key.Length * j + k;
                        string currentcipher = ciphertextsplited[num+1];
                        string currentkey = parseddkey.Substring(2*k,2);
                        parseddmessage = parseddmessage + decrypt(currentkey, currentcipher);
                    }
                }
                for (int i = 0; i < parseddmessage.Length / 2; i++)
                {
                      string currentparsedmessage = parseddmessage.Substring(2*i,2);
                      message =message+ Enum.GetName(typeof(convertable), int.Parse(currentparsedmessage));
                }
                string finalmessage = message.Replace("A", "");
                Console.WriteLine("Decrypted Message is");
                Console.WriteLine(finalmessage);
                Console.ReadLine();

            }
            if (mode == "2")
            {
                Console.WriteLine("Enter Message you want to encrypt");
                message = Console.ReadLine();
                char[] messages = message.ToCharArray();
                Console.WriteLine("Enter EncryptionKey");
                key = Console.ReadLine();
                int number = 0;
                number = key.Length;
                char[] keys = key.ToCharArray();
                string parsedmessage = null;
                string parsedkey = null;
                int groupnumber = 1+(message.Length / key.Length);
                for (int i = 0; i < message.Length; i++)
                {
                    if (messages[i] == char.Parse(" "))
                    {
                        messages[i] = char.Parse("A"); 
                    }
                    parsedmessage = parsedmessage + convert(messages[i]);
                }
                for (int i = 0; i < key.Length; i++)
                {
                    parsedkey = parsedkey + convert(keys[i]);
                }
                int messagelength = parsedmessage.Length;
                for (int i = 0; i < (2 * groupnumber * number - messagelength); i++)
                {
                    parsedmessage = parsedmessage + "0000";
                }
                for (int i = 0; i < groupnumber; i++)
                {
                    string messagegroup = parsedmessage.Substring(2 * number * i, 2 * number);
                    ciphertext = ciphertext + cipher(parsedkey, messagegroup, number);
                }

                Console.WriteLine("CipherText is:");
                Console.WriteLine(ciphertext);
                Console.ReadLine();
            }

        }
    }
}
